package com.ego.dubbo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ego.dubbo.service.ContentCategoryDubboService;
import com.ego.exception.DaoException;
import com.ego.mapper.ContentCategoryMapper;
import com.ego.pojo.ContentCategory;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@DubboService
public class ContentCategoryDubbiServiceImpl implements ContentCategoryDubboService {

    @Autowired
    private ContentCategoryMapper contentCategoryMapper;

    @Override
    public ContentCategory selectContentCategoryById(Long id) {
        return contentCategoryMapper.selectById(id);
    }

    @Override
    public List<ContentCategory> selectContentCategoryByParentId(Long id) {
        QueryWrapper<ContentCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", id);
        queryWrapper.eq("status",1);
        return contentCategoryMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public int saveContentCategory(ContentCategory contentCategory) throws DaoException {
        //判断当前新增节点的父节点是否还有字节点，如果无则修改父节点的是否父节点状态
        Long parentId = contentCategory.getParentId();
        ContentCategory contentCategoryFather = contentCategoryMapper.selectById(parentId);
        if (!contentCategoryFather.getIsParent()) {
            contentCategoryFather.setIsParent(true);
            contentCategoryFather.setUpdated(contentCategory.getCreated());
            int i = contentCategoryMapper.updateById(contentCategoryFather);
            if (i != 1) {
                throw new DaoException("修改父节点状态失败");
            }
        }

        /*List<ContentCategory> contentCategories = selectContentCategoryByParenId(parentId);
        if (contentCategories.size() == 0) {
            ContentCategory contentCategoryFather = new ContentCategory();
            contentCategoryFather.setId(parentId);
            contentCategoryFather.setIsParent(true);
            contentCategoryFather.setUpdated(contentCategory.getCreated());

            //修改父节点
            int i = contentCategoryMapper.updateById(contentCategoryFather);
            if (i != 1) {
                throw new DaoException("修改父节点状态失败");
            }
        }*/


        int result = contentCategoryMapper.insert(contentCategory);
        if (result == 1) {
            return 1;
        }
        throw new DaoException("新增节点失败");
    }

    @Override
    public int updateContentCategoryById(ContentCategory contentCategory) {
        return contentCategoryMapper.updateById(contentCategory);
    }

    @Override
    @Transactional
    public int deleteContentCategoryById(long id) {
        Date now = new Date();
        //1.删除当前节点及递归删除下面的所有子节点
        int result = updateStatusById(id, now);

        //2.判断删除当前节点的父节点是否还有其他子节点
        //查询当前节点对象
        ContentCategory curr = selectContentCategoryById(id);
        //查询父节点的所有字节点集合
        List<ContentCategory> sons = selectContentCategoryByParentId(curr.getParentId());
        if (sons.size() == 0){
            ContentCategory father = new ContentCategory();
            father.setId(curr.getParentId());
            father.setIsParent(false);
            father.setUpdated(now);
            int fatherResult = contentCategoryMapper.updateById(father);
            if(fatherResult != 1){
                throw new DaoException("更新父节点状态失败");
            }
        }
        return 1;
    }

    /**
     * 删除传入的当前节点，递归的删除所有子节点
     *
     * @param id
     * @return
     */
    public int updateStatusById(long id, Date now) {
        //删除一个节点
        ContentCategory contentCategory = new ContentCategory();
        contentCategory.setId(id);
        contentCategory.setStatus(2);
        contentCategory.setUpdated(now);
        System.out.println("预备更新...");
        int result = contentCategoryMapper.updateById(contentCategory);

        if (result == 1) {
            //所有的子节点
            List<ContentCategory> list = selectContentCategoryByParentId(id);
            int count = 0;
            for (ContentCategory son : list) {
                System.out.println("要递归了！！！");
                count += updateStatusById(son.getId(), now);
            }
            //1.传入的节点，没有子节点。 2.传入的节点有子节点，并把所有子节点统统删掉
            //递归的结束条件
            if (list.size() == 0 || count == list.size()) {
                return 1;
            }
        }
        throw new DaoException("删除内容管理节点失败");
    }
}
